package com.riddle.admin.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.riddle.model.Menu;
import com.riddle.model.Permission;
import com.riddle.model.TreeModel;
import com.riddle.service.BaseService;
import com.riddle.service.PermissionService;
import com.riddle.util.JsonUtil;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.PageModel;
import com.riddle.vmodel.QueryModel;
import com.riddle.vmodel.VTreeModel;

@Controller
@RequestMapping("/admin/permission")
public class PermissionController extends BaseController<Permission> {

	@RequestMapping()
	public String index() {
		return "admin/permission/index";
	}
	
	@Resource
	private PermissionService permissionService;
	
	protected BaseService getService() {
		return permissionService;
	}
	
	@RequestMapping(value="/list", produces="application/json;charset=utf-8")
	@ResponseBody
	public String list(QueryModel queryModel) {
		return super.list(queryModel);
	}
	
	@RequestMapping("/add")
	public String add() {
		return "admin/permission/add";
	}
	
	@RequestMapping(value="/addmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addModel(Permission permission) {
		return super.addModel(permission);
	}
	
	@RequestMapping("/modify")
	public ModelAndView modify(Long id) {
		Permission permission = permissionService.query(id);
		return new ModelAndView("admin/permission/modify", "permission", permission);
	}
	
	@RequestMapping(value="/modifymodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String modifyModel(Permission permission) {
		return super.modifyModel(permission);
	}
	
	@RequestMapping(value="/delete", produces="application/json;charset=utf-8")
	@ResponseBody
	public String delete(Long id) {
		return super.delete(id);
	}
	
	@RequestMapping(value="/getparentmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String getParentModel() {
		return super.getParentModel();
	}
	
	@RequestMapping(value="/tree", produces="application/json;charset=utf-8")
	@ResponseBody
	public String tree() {
		List<Permission> list = permissionService.getPermissionTree();
		List<VTreeModel> res = VTreeModel.toVTreeModel(list);
		return JsonUtil.toJson(res);
	}
}
